package com.heima.controller;

import com.github.pagehelper.util.StringUtil;
import com.heima.pojo.dto.NodeDto;
import com.heima.pojo.vo.NodeDetailVo;
import com.heima.pojo.vo.NodeMachineDetailVo;
import com.heima.service.NodeService;
import com.heima.utils.PageResult;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 赵超
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/vm-service/node")
public class NodeController {
    private final NodeService nodeService;

    @GetMapping("/search")
    public PageResult<NodeDetailVo> getRegionById(
            String name,
            String regionId,
            Integer pageIndex,
            Integer pageSize) {
        return nodeService.getRegionById(name, StringUtil.isEmpty(regionId) ? null : Long.valueOf(regionId), pageIndex, pageSize);
    }

    @PostMapping
    public Boolean add(@RequestBody NodeDto nodeDto) {
        return nodeService.add(nodeDto) > 0;
    }

    @GetMapping("/vmList/{id}")
    public List<NodeMachineDetailVo> getVmList(@PathVariable Integer id) {
        return nodeService.getVmList(id);
    }

    @PutMapping("/{id}")
    public Boolean update(@PathVariable Integer id, @RequestBody NodeDto nodeDto) {
        return nodeService.update(id, nodeDto) > 0;
    }

    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return nodeService.delete(id) > 0;

    }

    @GetMapping("/count")
    public Integer getNodeCount() {
        return nodeService.count();
    }

    @GetMapping("/nodeCollect")
    public Integer getNodeCollect() {
        return 111;
    }
}
